$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/bbeginnerarticletype/queryList',
        datatype: "json",
        colModel: [			
			{ label: 'ID', name: 'id', index: 'id',align: "center", width: 50, key: true },
			{ label: '标签名称', name: 'name', index: 'name',align: "center", width: 80 },
			{ label: '公众号', name: 'gzhNames', index: 'gzhNames',align: "center", width: 120 },
			{ label: '状态', name: 'status', index: 'status',align: "center", width: 60, formatter: function (value,options,rowData) {
					if (value == 0) {
						return "<p style='color: red'>删除</p>";
					} else if (value == 1) {
						return "正常";
					}
				}
			},
			{ label: '创建时间', name: 'createTime', index: 'create_time',align: "center", width: 80 },
			{ label: '修改时间', name: 'modifyTime', index: 'modify_time',align: "center", width: 80 }, 			
        ],
		viewrecords: true,
        height: 520,
        rowNum: 15,
		rowList : [15,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
		postData:{"gzhCode":vm.q.gzhCode,"id":vm.q.id,"name":vm.q.name},
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });

	$.ajax({
		type: "POST",
		async:false,
		url: baseURL + 'sys/buserinfo/getUserGzhList',
		contentType: "application/json",
		success: function(r){
			if(r.code==0){
				//vm.bbrandinfo=r.brandInfo;
				$("#selectUserGzhList").append(
					"<option value=''>全部平台</option>");
				for(var i=0;i<r.userGzhList.length;i++){
					$("#selectUserGzhList").append(
						"<option value=" + r.userGzhList[i].gzhCode +">"+ r.userGzhList[i].gzhName+"</option>");
				}
			}
		}
	});
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			gzhCode: '',
			id: null,
			name: null
		},
		showList: true,
		title: null,
		bBeginnerArticleType: {},
		gzhList:{},
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.bBeginnerArticleType = {};
			vm.bBeginnerArticleType.status = 1;
			vm.bBeginnerArticleType.gzhCodeList = [];

			//获取公众号信息
			this.getGzhList();
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)

			//获取公众号信息
			this.getGzhList();
		},
		saveOrUpdate: function (event) {
			var url = vm.bBeginnerArticleType.id == null ? "sys/bbeginnerarticletype/save" : "sys/bbeginnerarticletype/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.bBeginnerArticleType),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/bbeginnerarticletype/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "sys/bbeginnerarticletype/info/"+id, function(r){
                vm.bBeginnerArticleType = r.bBeginnerArticleType;
            });
		},
		getGzhList: function(){
			$.get(baseURL + "sys/user/selectGzhList", function(r){
				vm.gzhList = r.list;
			});
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
				postData:{"gzhCode":vm.q.gzhCode,"id":vm.q.id,"name":vm.q.name},
				page:page
            }).trigger("reloadGrid");
		}
	}
});